import { Column, Entity, Index, JoinColumn, ManyToOne, OneToOne } from 'typeorm';
import { CommonEntity } from '~/common/entity/common.entity';
import { UserEntity } from '../user/user.entity';
import { Exclude } from 'class-transformer';
import { TenantEntity } from '../tenant/tenant.entity';

@Entity('ah_points')
@Index(['id'])
export class PointsEntity extends CommonEntity {
  @Column({
    type: 'bigint',
    default: 0,
    comment: '积分额度',
    transformer: {
      from: (value: string): number => parseInt(value, 10),
      to: (value: number): number => value,
    },
  })
  value: number;

  // 备注
  @Column({ type: 'varchar', length: 255, nullable: true, default: null, comment: '积分变动备注' })
  remark: string;

  @OneToOne(() => UserEntity, (user) => user.points)
  @JoinColumn({ name: 'user_id' })
  user: UserEntity;

  @Exclude()
  @ManyToOne(() => TenantEntity, (tenant) => tenant.points, { nullable: true })
  @JoinColumn({ name: 'tenant_id' })
  tenant: TenantEntity;
}
